<template>
  <div>
    <el-form label-width="100px">
      <el-form-item label="上级考点">
        {{parentName}}
      </el-form-item>
      <el-form-item label="考点名称">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="form.description" type="textarea" :rows="4"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submit" type="primary">保存并返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  data() {
    return {
      parentName: this.$route.query.parentName || '顶级分类', // 上级分类名称
      form: {
        id: this.$route.query.id || 0, // 要修改的记录的ID，0：添加,非0代表是修改
        parent_id: this.$route.query.parent_id || 0, // 如果没传就是0，否则就用传的值
        name: '',
        description: ''
      }
    }
  },
  created() {
    // 判断如果是修改，需要使用 id 获取这条记录的详细信息填充到表单中
    if (this.form.id > 0) {
      axios.get('/exam/point/' + this.form.id).then(res => {
        if (res) {
          // 把数据填充到表单中
          this.form.name = res.data.name
          this.form.description = res.data.description
        }
      })
    }
  },
  methods: {
    submit() {
      if (this.form.id > 0) {
        // 调用修改
        axios.put('/exam/point/' + this.form.id, this.form).then(res => {
          if (res) {
            this.$message.success('修改成功')
            this.$router.push({ name: 'QuestionQuestionCheckPointManage' })
          }
        })
      } else {
        // 添加接口
        axios.post('/exam/point', this.form).then(res => {
          if (res) {
            this.$message.success('添加成功')
            this.$router.push({ name: 'QuestionQuestionCheckPointManage' })
          }
        })
      }
    }
  }
}
</script>
